using CxRomos.Core.DataAccess;
using CxRomos.Core.Services;
using CxRomos.Web.Account.Interface;
using StructureMap;

namespace CxRomos.Web.Account.Presenter {
    public class RecoverPasswordPresenter {
        private IRecoverPassword _view;
        private readonly IEmail _email;
        private readonly IAccountRepository _accountRepository;

        public RecoverPasswordPresenter() {
            _email = ObjectFactory.GetInstance<IEmail>();
            _accountRepository = ObjectFactory.GetInstance<IAccountRepository>();
        }

        public void Init(IRecoverPassword view) {
            _view = view;
        }

        public void RecoverPassword(string email) {
            var account = _accountRepository.GetAccountByEmail(email);

            if(account != null) {
                _email.SendPasswordReminderEmail(account.Email, account.Password, account.Email);
                _view.ShowRecoverPasswordPanel(false);
                _view.ShowMessage("An email is sent to your account");
            } else {
                _view.ShowRecoverPasswordPanel(true);
                _view.ShowMessage("We couldn't find the account you requested");
            }
        }
    }
}